﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.BasicData.Currencies;
using mozhi.smarterp.BasicData.Currencies.Repositories;
using mozhi.smarterp.BasicData.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.BasicData.Repositories
{
    public class CurrencyRepository : EfCoreRepository<BasicDataDbContext, Currency, Guid>, ICurrencyRepository
    {
        public CurrencyRepository(IDbContextProvider<BasicDataDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        public async Task<long> CountAsync(string code, string name, List<Guid> excluedIds = null, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var count = await dbContext.Set<Currency>().AsNoTracking()
                .WhereIf(!code.IsNullOrEmpty(), x => x.Code == code)
                .WhereIf(!name.IsNullOrEmpty(), x => x.Name == name)
                .WhereIf(!excluedIds.IsNullOrEmpty(), x => !excluedIds.Contains(x.Id))
                .CountAsync();

            return count;
        }
    }
}
